package com.jlnk.androidfmc.fragment.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import androidx.activity.EdgeToEdge;
import androidx.fragment.app.Fragment;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.jlnk.androidfmc.FavoritesActivity;
import com.jlnk.androidfmc.FootActivity;
import com.jlnk.androidfmc.MainActivity;
import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.SettingsActivity;
import com.jlnk.androidfmc.ShoppingCartActivity;
import com.jlnk.androidfmc.Tools; // 确保这个工具类是存在的

public class ProfileFragment extends Fragment {

    private LinearLayout containerSettings;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    public ProfileFragment() {
        // Required empty public constructor
    }

    public static ProfileFragment newInstance(String param1, String param2) {
        ProfileFragment fragment = new ProfileFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            // 你的参数处理逻辑
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_profile, container, false);

        ImageButton shoppingCartButton = view.findViewById(R.id.shopping_cart);
        shoppingCartButton.setOnClickListener(v -> {
            Intent intent = new Intent(requireActivity(), ShoppingCartActivity.class);
            startActivity(intent);
        });
        // 初始化收藏按钮点击事件
        ImageButton collectButton = view.findViewById(R.id.collect);
        collectButton.setOnClickListener(v -> {
            Intent intent = new Intent(requireActivity(), FavoritesActivity.class);
            startActivity(intent);
        });

        // 在ProfileFragment中初始化足迹按钮并设置点击事件
        ImageButton footButton = view.findViewById(R.id.foot);
        footButton.setOnClickListener(v -> {
            Intent intent = new Intent(requireActivity(), FootActivity.class);
            startActivity(intent);
        });

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化视图
        containerSettings = view.findViewById(R.id.settings_container);

        // 设置点击事件
        containerSettings.setOnClickListener(v -> {
            Intent intent = new Intent(requireActivity(), SettingsActivity.class);
            startActivity(intent);
        });

        // 通知 MainActivity 在非首页隐藏"去卖货"按钮和图片
        MainActivity mainActivity = (MainActivity) getActivity();
        if (mainActivity != null) {
            mainActivity.setGoSellVisibility(false);
        }

        // 设置 EdgeToEdge 效果
        ViewCompat.setOnApplyWindowInsetsListener(view, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
}